/**
 * Created by Administrator on 2015/3/15.
 */
angular.module('layoutDirective', ['layoutFactory'])
    //总导航控制指令 ,点击横向导航，显示纵向导航
    .directive('nav', function ($timeout,$cookieStore,$cookies) {
        return {
            link: function (scope, element, attr) {
                if (attr.nav == 'init') {
                    alert('init 为初始化变量，您不能使用');
                }
                ////初始化一个导航显示
                //if (scope.nav.init == false) {
                //    scope.nav.init = true;
                //    scope.nav[attr.nav] = true;
                //}
                element.bind('click', function () {
                    angular.forEach(scope.nav, function (item, key) {
                        scope.nav[key] = false;
                    });
                    $timeout(function () {
                        scope.nav[attr.nav] = true;
                        //记录导航状态
                        $cookieStore.put('navInfo',scope.nav);
                        //设置主体右边框
                        setHeight();
                    });
                });
            }
        }
    }
)
    //点击纵向导航增加当前样式
    .directive('navList', function () {
        return {
            link: function (scope, element, attr) {
                element.find('a').bind('click', function () {
                    _$(this).parent('div').find('a').removeClass('active');
                    _$(this).addClass('active');
                });
            },
            templateUrl: TPLROOT + 'public/nav_list.html'
        }
    })
    //重复值验证，调用格式 repeatpwd=“formName.inputName” 【repeatpwd=“表单name.输入框name”】
    .directive('repeatval', function () {
        return {
            require: 'ngModel',
            link: function (scope, elm, attrs, ctrl) {
                var tageCtrl = scope.$eval(attrs.repeatval);
                tageCtrl.$parsers.push(function (viewValue) {
                    ctrl.$setValidity('repeatval', viewValue == ctrl.$viewValue);
                    return viewValue;
                });
                ctrl.$parsers.push(function (viewValue) {
                    if (viewValue == tageCtrl.$viewValue) {
                        ctrl.$setValidity('repeatval', true);
                        return viewValue;
                    } else {
                        ctrl.$setValidity('repeatval', false);
                        return undefined;
                    }
                });
            }
        };
    })
    //表单验证指令，调用格式 form-check="obj" [obj 是 $scope下面的一个对象]
    .directive('formCheck', function (form, $timeout) {
        return {
            link: function (scope, element, attrs) {
                var info = {};
                //验证属性配置
                var validate = ['required', 'ng-required', 'ng-minlength', 'ng-maxlength', 'pattern', 'ng-pattern', 'repeatval','min','max'];
                //循环记录验证属性
                angular.forEach(element[0], function (item) {
                    var i_name = _$(item)[0].localName;//输入框标签名称
                    var i_type = _$(item).attr('type');//输入框type
                    if (i_type == 'button' || i_type == 'hidden') return; //按钮隐藏域跳过
                    if (i_name == 'button') return;// 按钮跳过
                    //循环验证属性，取得设置的验证值
                    var tmp = {};
                    for (var i = 0, len = validate.length; i < len; i++) {
                        var attr_value = _$(item).attr(validate[i]);
                        if (attr_value == undefined || attr_value == 'undefined') continue;
                        //分割验证属性ng-minlength 变成 minlength
                        var check_type = validate[i].split('-');
                        check_type = (check_type.length > 1) ? check_type[1] : check_type[0];
                        tmp[check_type] = attr_value;
                    }
                    tmp['name'] = _$(item).parent('div').find('label').text();
                    info[_$(item).attr('name')] = tmp;
                });
                var formName = attrs.name;//表单Name
                //监听表单对象，启动验证
                scope.$watch(attrs.formCheck, function (newValue, oldValue) {
                    if (newValue == oldValue) return false;
                    scope.info = form.check(info, scope[formName]);
                }, true);
                console.log('如果写了验证规则，不起作用，请检查是否给表单赋值了ng-model,设置格式为 [obj.input]。label不边颜色，请查看label是否加了control-label');
            }
        }
    });